package com.gc.cscott.domain.result;

import com.gc.cscott.domain.contest.TestCase;
import com.gc.cscott.domain.contest.numbers.TaskNumber;
import com.gc.cscott.domain.contest.numbers.TestCaseNumber;
import com.gc.cscott.domain.Competitor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 * @author root
 */
public class CompetitorResult {
    String competitorName;
    String timeMark;
    List<TestCaseResult> results = Collections.emptyList();

    public CompetitorResult() {
    }

    
    public CompetitorResult(Competitor competitor) {
        this.competitorName = competitor.getName();
        this.timeMark="none";
    }

    public String getCompetitorName() {
        return competitorName;
    }

    public void setCompetitorName(String competitorName) {
        this.competitorName = competitorName;
    }

    public String getTimeMark() {
        return timeMark;
    }

    public void setTimeMark(String timeMark) {
        this.timeMark = timeMark;
    }

    public void addResult(TestCaseResult result){
        results = new ArrayList<TestCaseResult>(results);
        results.add(result); //should be fine as this structure is updeted one by one.
    }

    public List<TestCaseResult> getResults() {
        if (results==null){
            results = new ArrayList<TestCaseResult>();
        }
        return results;
    }

    public TestCaseResult getResult(TaskNumber taskNumber, TestCaseNumber testCaseNumber){
        for(TestCaseResult result: getResults()){
            if (result.getTaskNumber().intValue()==taskNumber.intValue()
                    && result.getTestCaseNumber().intValue()==testCaseNumber.intValue()){
                return result;
            }
        }
        return null;
    }

    public TestCaseResult getResult(TestCase testCase){
        return getResult(testCase.getTask().getNumber(), testCase.getNumber());
    }

    public int getCountOfPassedTests() {
        int totalPassed = 0;
        for(TestCaseResult tcr:getResults()){
            if (tcr.getResult()==TestCaseResult.HowEnded.Correct){
                totalPassed++;
            }
        }
        return totalPassed;
    }


}
